package com.atguigu.service.impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private AdminRoleDao adminRoleDao;

    // 为父类中抽取的公共方法提供一个具体的dao对象
    @Override
    protected BaseDao<Role> getEntityDao() {
        return roleDao;
    }
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Map<String, Object> findRoleByAdminId(Long adminId) {
//        查询所有角色
        List<Role> roleList = roleDao.findAll();
//         拥有的角色id
        List<Long> roleListId = adminRoleDao.findRoleIdByAdminId(adminId);

        //对角色进行分类
        List<Role> noassignRoleList = new ArrayList<>();
        List<Role> assignRoleList = new ArrayList<>();

        for (Role role : roleList) {
            if (roleListId.contains(role.getId())) {
                assignRoleList.add(role);
            } else {
                noassignRoleList.add(role);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("noassignRoleList", noassignRoleList);
        map.put("assignRoleList", assignRoleList);
        return map;
    }


}
